package com.eboss.sbs.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.eboss.sbs.R;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.myapplication.MyApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.mytools.tool.utils.NetworkUtil;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;



@SuppressWarnings("rawtypes")
public class HttpRequest2 {

	private AsyncHttpClient client;
	private Context context;
	private TextHttpResponseHandler responseHandler;
	private HttpCallback callback;

	@SuppressWarnings("static-access")
	public HttpRequest2(final Context context, final HttpCallback callback) {
		this.context = context;
		//this.client = new AsyncHttpClient();
		this.client = MyApplication.getmInstance().getHttpclient();
		
		this.callback = callback;
		this.client.setTimeout(Config.HTTP_TIME_OUT);
		
		this.responseHandler = new TextHttpResponseHandler() {
			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				if(callback == null){return;}
				callback.onFailure(statusCode, context.getString(R.string.load_data_failed));
			}
			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				if(Config.PRINT_LOG){Log.e("http","responseString:"+ responseString);}
				if(callback == null){return;}
				HttpResponse2 data;
				try {
					data = null == callback.getTypeToken() ? HttpJsonParser2.fromJson(responseString) : HttpJsonParser2.fromJson(responseString, callback.getTypeToken());
				} catch (Exception e) {
					Log.e("parse_data_failed", "parse_data_failed..."+e.getMessage());
					callback.onFailure(statusCode, context.getString(R.string.parse_data_failed));
					return;
				}
				if (data.getCode()==Config.HTTP_OK_2) {callback.onSuccess(statusCode, data.getResult());}
				else if (!TextUtils.isEmpty(data.getMessage())) {
					callback.onFailure(statusCode, data.getMessage());
				}
			}
			@Override
			public void onFinish() {
				if(callback == null){return;}
				callback.postLoad();
			}
			@Override
			public void onStart() {
				if(callback == null){return;}
				callback.preLoad();
			}
		};
		
	}

	public void get(String url) {
		if (!NetworkUtil.isNetworkAvailable(context)) {
			if(Config.PRINT_LOG){Log.e("http", url);}
			if(callback == null){return;}
			callback.onFailure(-1, context.getString(R.string.network_info));
			return;
		}
		client.get(url, responseHandler);
	}
	
	public void get(String url, RequestParams params) {
		if (!NetworkUtil.isNetworkAvailable(context)) {
			if(callback == null){return;}
			callback.onFailure(-1, context.getString(R.string.network_info));
			return;
		}
		client.get(url,params, responseHandler);
	}

	public void post(String url, RequestParams requestParams) {
		if (!NetworkUtil.isNetworkAvailable(context)) {
			if(callback == null){return;}
			callback.onFailure(-1, context.getString(R.string.network_info));
			return;
		}
		client.post(url, requestParams, responseHandler);
	}

	public void post(String url, HttpEntity entity) {
		if (!NetworkUtil.isNetworkAvailable(context)) {
			if(callback == null){return;}
			callback.onFailure(-1, context.getString(R.string.network_info));
			return;
		}
		client.post(context, url, entity, null, responseHandler);
	}
	

//	public void post(String url, Map<String, Object> params) {
//		JSONObject jsonParams = new JSONObject(params);
//		StringEntity entity;
//		try {
//			if(Config.PRINT_LOG){
//				Log.e("http", url);
//				Log.e("http", jsonParams.toString());
//			}
//			entity = new StringEntity(jsonParams.toString(), "UTF-8");
//			entity.setContentType("application/json;charset=UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			if(callback == null){return;}
//			callback.onFailure(context.getString(R.string.load_data_failed));
//			return;
//		}
//		post(url, entity);
//	}
	
	//POST
	public void post(String url, Map<String, Object> params) {
		JSONObject jsonParams = new JSONObject(params);
		StringEntity entity;
		try {
			if(Config.PRINT_LOG){
				Log.e("http", url);
				Log.e("http", jsonParams.toString());
			}
			entity = new StringEntity(jsonParams.toString(), "UTF-8");
			entity.setContentType("application/json;charset=UTF-8");
		} catch (UnsupportedEncodingException e) {
			if(callback == null){return;}
			callback.onFailure(-1, context.getString(R.string.load_data_failed));
			return;
		}
		post(url, entity);
	}
	
//	public void post_upgrade_pre(String url, Map<String, Object> params) {
//		JSONObject jsonParams = new JSONObject(params);
//		StringEntity entity;
//		try {
//			if(Config.PRINT_LOG){
//				Log.e("http", url);
//				Log.e("http", jsonParams.toString());
//			}
//			entity = new StringEntity(jsonParams.toString(), "UTF-8");
//			entity.setContentType("application/json;charset=UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			if(callback == null){return;}
//			callback.onFailure(context.getString(R.string.load_data_failed));
//			return;
//		}
//		post(url, entity);
//	}
	
//	public void postByArrParam(String url,String content) {
//		StringEntity entity;
//		try {
//			entity = new StringEntity(content, "UTF-8");
//			entity.setContentType("application/json;charset=UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			if(callback == null){return;}
//			callback.onFailure(context.getString(R.string.load_data_failed));
//			return;
//		}
//		post(url, entity);
//	}
//	
//	public void post(String url, Map<String, Object> params, Boolean is) {
//		Gson gson = new Gson();
//		String gsonString = gson.toJson(params);
//		StringEntity entity;
//		try {
//			entity = new StringEntity(gsonString, "UTF-8");
//			entity.setContentType("application/json;charset=UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			if(callback == null){return;}
//			callback.onFailure(context.getString(R.string.load_data_failed));
//			return;
//		}
//		post(url, entity);
//	}
//	
//	public void post(String url) {
//		HttpEntity entity = null;
//		if(Config.PRINT_LOG){Log.e("http", url);}
//		post(url, entity);
//	}
	
}
